<template>
  <div class="divination-container">
    <div class="header">
      <div class="header-content">
        <h1 class="main-title">
          <span class="title-icon">🪙</span>
          <span class="title-text">易经算卦</span>
        </h1>
        <p class="subtitle">诚心问卦，顺应天意</p>
        <div class="divider"></div>
      </div>
    </div>

    <div class="question-section">
      <h2>请提出您的问题</h2>
      <textarea 
        v-model="question" 
        placeholder="请输入您想要询问的问题..."
        class="question-input"
        rows="3"
      ></textarea>
    </div>

    <div class="method-section">
      <h2>选择算卦方式</h2>
      <div class="method-buttons">
        <button 
          @click="performDivination('coins')" 
          :disabled="loading"
          class="method-btn coins-btn"
        >
          <span class="icon">🪙</span>
          三枚硬币
        </button>
        <button 
          @click="performDivination('number')" 
          :disabled="loading"
          class="method-btn number-btn"
        >
          <span class="icon">🔢</span>
          数字随机
        </button>
        <button 
          @click="performDivination('random')" 
          :disabled="loading"
          class="method-btn random-btn"
        >
          <span class="icon">🎲</span>
          随机生成
        </button>
      </div>
    </div>

    <div v-if="loading" class="loading">
      <div class="spinner"></div>
      <p>{{ loadingText }}</p>
    </div>

    <div v-if="result" class="result-section">
      <div class="hexagram-display">
        <div class="hexagram-symbol">
          <div class="hexagram-lines">
            <div v-for="(line, index) in getHexagramLines(result.hexagram.symbol)" :key="index" class="hexagram-line">
              {{ line }}
            </div>
          </div>
        </div>
        <div class="hexagram-info">
          <h3>{{ result.hexagram.chineseName }} - {{ result.hexagram.name }}</h3>
          <p class="description">{{ result.hexagram.description }}</p>
          <p class="interpretation">{{ result.hexagram.interpretation }}</p>
        </div>
      </div>

      <div class="interpretation-section">
        <h3>AI智能解读</h3>
        <div class="ai-badge">🤖 AI解读</div>
        <div class="interpretation-text">{{ result.interpretation }}</div>
      </div>

      <div class="lines-section">
        <h3>爻辞</h3>
        <div class="lines">
          <div v-for="(line, index) in result.hexagram.lines" :key="index" class="line">
            {{ line }}
          </div>
        </div>
      </div>

      <button @click="clearResult" class="clear-btn">重新算卦</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const question = ref('')
const loading = ref(false)
const loadingText = ref('正在为您算卦...')
const result = ref(null)

const performDivination = async (method) => {
  loading.value = true
  loadingText.value = '正在生成卦象...'
  result.value = null

  try {
    const params = new URLSearchParams()
    if (question.value) {
      params.append('question', question.value)
    }
    params.append('method', method)

    // 先获取卦象
    const response = await fetch(`http://localhost:3000/yijing/divination?${params}`)
    const data = await response.json()
    
    // 更新加载文本
    loadingText.value = 'AI正在解读卦象...'
    
    // 模拟AI解读的延迟效果
    await new Promise(resolve => setTimeout(resolve, 2000))
    
    result.value = data
  } catch (error) {
    console.error('算卦失败:', error)
    alert('算卦失败，请重试')
  } finally {
    loading.value = false
    loadingText.value = '正在为您算卦...'
  }
}

const clearResult = () => {
  result.value = null
  question.value = ''
}

const getHexagramLines = (symbol) => {
  // 将卦象符号转换为6爻显示
  const lines = []
  
  // 解析上卦和下卦
  const upperTrigram = symbol[0]
  const lowerTrigram = symbol[1]
  
  // 八卦对应的三爻
  const trigramLines = {
    '☰': ['━━━', '━━━', '━━━'], // 乾
    '☷': ['━ ━', '━ ━', '━ ━'], // 坤
    '☳': ['━━━', '━ ━', '━ ━'], // 震
    '☴': ['━ ━', '━━━', '━━━'], // 巽
    '☵': ['━ ━', '━━━', '━ ━'], // 坎
    '☲': ['━━━', '━ ━', '━━━'], // 离
    '☶': ['━ ━', '━ ━', '━━━'], // 艮
    '☱': ['━━━', '━━━', '━ ━']  // 兑
  }
  
  // 组合成六爻（上卦在上，下卦在下）
  const upperLines = trigramLines[upperTrigram] || ['━ ━', '━ ━', '━ ━']
  const lowerLines = trigramLines[lowerTrigram] || ['━ ━', '━ ━', '━ ━']
  
  // 从上到下排列（上九、九五、九四、九三、九二、初九）
  lines.push(upperLines[2]) // 上九
  lines.push(upperLines[1]) // 九五
  lines.push(upperLines[0]) // 九四
  lines.push(lowerLines[2]) // 九三
  lines.push(lowerLines[1]) // 九二
  lines.push(lowerLines[0]) // 初九
  
  return lines
}
</script>

<style scoped>
.divination-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Microsoft YaHei', sans-serif;
}

.header {
  text-align: center;
  margin-bottom: 50px;
  position: relative;
  padding: 40px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 20px;
  color: white;
  overflow: hidden;
}

.header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="50" cy="50" r="1" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  opacity: 0.3;
}

.header-content {
  position: relative;
  z-index: 2;
}

.main-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
  font-size: 3rem;
  margin-bottom: 15px;
  font-weight: 800;
}

.title-icon {
  font-size: 3.5rem;
  animation: coinSpin 3s ease-in-out infinite;
}

.title-text {
  background: linear-gradient(45deg, #fff, #f0f8ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 4px 8px rgba(0,0,0,0.3);
}

.subtitle {
  font-size: 1.3rem;
  opacity: 0.9;
  font-style: italic;
  margin-bottom: 20px;
  font-weight: 300;
}

.divider {
  width: 100px;
  height: 3px;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.8), transparent);
  margin: 0 auto;
  border-radius: 2px;
}

@keyframes coinSpin {
  0%, 100% { transform: rotateY(0deg); }
  50% { transform: rotateY(180deg); }
}

.question-section, .method-section {
  margin-bottom: 50px;
  background: white;
  padding: 30px;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  border: 1px solid rgba(255,255,255,0.2);
}

.question-section h2, .method-section h2 {
  color: #2c3e50;
  margin-bottom: 25px;
  font-size: 1.8rem;
  font-weight: 700;
  text-align: center;
  position: relative;
}

.question-section h2::after, .method-section h2::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  border-radius: 2px;
}

.question-input {
  width: 100%;
  padding: 20px;
  border: 2px solid #e1e8ed;
  border-radius: 15px;
  font-size: 1.1rem;
  resize: vertical;
  transition: all 0.3s ease;
  background: #f8f9fa;
  font-family: inherit;
  line-height: 1.6;
}

.question-input:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  transform: translateY(-2px);
}

.question-input::placeholder {
  color: #a0aec0;
  font-style: italic;
}

.method-buttons {
  display: flex;
  gap: 25px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 20px;
}

.method-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 25px 20px;
  border: none;
  border-radius: 20px;
  color: white;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  min-width: 140px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}

.method-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s;
}

.method-btn:hover::before {
  left: 100%;
}

.method-btn:hover:not(:disabled) {
  transform: translateY(-5px) scale(1.05);
  box-shadow: 0 15px 35px rgba(0,0,0,0.25);
}

.method-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.method-btn .icon {
  font-size: 2.5rem;
  margin-bottom: 15px;
  transition: transform 0.3s;
}

.method-btn:hover .icon {
  transform: scale(1.2) rotate(10deg);
}

.coins-btn {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.coins-btn:hover:not(:disabled) {
  box-shadow: 0 15px 35px rgba(240, 147, 251, 0.4);
}

.number-btn {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.number-btn:hover:not(:disabled) {
  box-shadow: 0 15px 35px rgba(79, 172, 254, 0.4);
}

.random-btn {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.random-btn:hover:not(:disabled) {
  box-shadow: 0 15px 35px rgba(67, 233, 123, 0.4);
}

.loading {
  text-align: center;
  padding: 60px 40px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  margin: 30px 0;
}

.spinner {
  width: 60px;
  height: 60px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 25px;
  position: relative;
}

.spinner::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 50%;
  animation: pulse 1.5s ease-in-out infinite;
}

.loading p {
  font-size: 1.2rem;
  color: #2c3e50;
  font-weight: 500;
  margin: 0;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 0.7; }
  50% { transform: translate(-50%, -50%) scale(1.2); opacity: 1; }
}

.result-section {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 25px;
  padding: 40px;
  margin-top: 40px;
  box-shadow: 0 15px 40px rgba(0,0,0,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  position: relative;
  overflow: hidden;
}

.result-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea, #764ba2, #f093fb, #f5576c);
  background-size: 200% 100%;
  animation: gradientShift 3s ease-in-out infinite;
}

@keyframes gradientShift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.hexagram-display {
  display: flex;
  align-items: center;
  gap: 40px;
  margin-bottom: 40px;
  padding: 30px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  position: relative;
  overflow: hidden;
}

.hexagram-display::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, transparent 30%, rgba(102, 126, 234, 0.05) 50%, transparent 70%);
  pointer-events: none;
}

.hexagram-symbol {
  color: #2c3e50;
  font-weight: bold;
  position: relative;
  z-index: 2;
}

.hexagram-lines {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 20px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 15px;
  border: 2px solid rgba(102, 126, 234, 0.1);
  box-shadow: inset 0 2px 10px rgba(0,0,0,0.05);
}

.hexagram-line {
  font-family: 'Courier New', monospace;
  font-size: 2rem;
  color: #2c3e50;
  line-height: 1;
  font-weight: bold;
  text-shadow: 0 1px 2px rgba(0,0,0,0.1);
  transition: color 0.3s ease;
}

.hexagram-line:hover {
  color: #667eea;
}

.hexagram-info h3 {
  color: #2c3e50;
  margin-bottom: 10px;
  font-size: 1.8rem;
}

.description {
  color: #7f8c8d;
  font-size: 1.1rem;
  margin-bottom: 10px;
}

.interpretation {
  color: #27ae60;
  font-weight: bold;
  font-size: 1.2rem;
}

.interpretation-section, .lines-section {
  margin-bottom: 40px;
  position: relative;
  z-index: 2;
}

.interpretation-section h3, .lines-section h3 {
  color: #2c3e50;
  margin-bottom: 20px;
  font-size: 1.6rem;
  font-weight: 700;
  text-align: center;
  position: relative;
}

.interpretation-section h3::after, .lines-section h3::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  border-radius: 2px;
}

.ai-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 8px 16px;
  border-radius: 25px;
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 20px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  animation: badgePulse 2s ease-in-out infinite;
}

@keyframes badgePulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.interpretation-text {
  background: white;
  padding: 30px;
  border-radius: 15px;
  line-height: 1.8;
  white-space: pre-line;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  font-size: 1.1rem;
  color: #2c3e50;
  position: relative;
  overflow: hidden;
}

.interpretation-text::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: linear-gradient(180deg, #667eea, #764ba2);
}

.lines {
  background: white;
  padding: 25px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  border: 1px solid rgba(255,255,255,0.2);
}

.line {
  padding: 12px 0;
  border-bottom: 1px solid #e9ecef;
  font-size: 1.1rem;
  color: #2c3e50;
  line-height: 1.6;
  transition: all 0.3s ease;
  position: relative;
  padding-left: 20px;
}

.line::before {
  content: '●';
  position: absolute;
  left: 0;
  color: #667eea;
  font-size: 0.8rem;
  top: 50%;
  transform: translateY(-50%);
}

.line:hover {
  background: rgba(102, 126, 234, 0.05);
  padding-left: 25px;
  border-radius: 8px;
}

.line:last-child {
  border-bottom: none;
}

.clear-btn {
  background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
  color: white;
  border: none;
  padding: 15px 35px;
  border-radius: 25px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  box-shadow: 0 8px 25px rgba(231, 76, 60, 0.3);
  position: relative;
  overflow: hidden;
}

.clear-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s;
}

.clear-btn:hover::before {
  left: 100%;
}

.clear-btn:hover {
  transform: translateY(-3px) scale(1.05);
  box-shadow: 0 15px 35px rgba(231, 76, 60, 0.4);
}

@media (max-width: 768px) {
  .main-title {
    font-size: 2.2rem;
    flex-direction: column;
    gap: 10px;
  }
  
  .title-icon {
    font-size: 2.5rem;
  }
  
  .method-buttons {
    flex-direction: column;
    align-items: center;
    gap: 15px;
  }
  
  .method-btn {
    width: 100%;
    max-width: 280px;
  }
  
  .hexagram-display {
    flex-direction: column;
    text-align: center;
    gap: 25px;
  }
  
  .hexagram-symbol {
    font-size: 3rem;
  }
  
  .hexagram-lines {
    padding: 15px;
  }
  
  .hexagram-line {
    font-size: 1.5rem;
  }
  
  .question-section, .method-section {
    padding: 20px;
  }
  
  .result-section {
    padding: 25px;
  }
  
  .interpretation-text {
    padding: 20px;
    font-size: 1rem;
  }
  
  .lines {
    padding: 20px;
  }
  
  .line {
    font-size: 1rem;
  }
}
</style>
